package com.jz.control;

import com.jz.entity.PageBean;
import com.jz.entity.ResultVo;
import com.jz.entity.User;
import com.jz.service.UserService;
import com.jz.service.impl.UserServiceImpl;
import com.jz.utils.RespJsonUtils;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Map;

/**
 * @ClassName UserServlet
 * @Description TODO
 * @Author MR_Liu
 * @Date 2021/7/22 0022 11:17
 * @Version 1.0
 */
@WebServlet("*.ur")
public class UserServlet extends BaseServlet {
    UserService userService = new UserServiceImpl();
    /*
    *@Date 2021/7/22 0022 11:30
    *@Description 用户登录
    */
    public void userLogin(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        //获取登录表单中的数据
        String username = req.getParameter("username");
        System.out.println("username = " + username);
        String password = req.getParameter("password");
        System.out.println("password = " + password);
        String imageCode = req.getParameter("imageCode");
        System.out.println("自己的:"+imageCode);



        //1.先校验验证码是否正确
        HttpSession session = req.getSession();
        //获取系统生成的验证码
        String code = (String)session.getAttribute("code");
        System.out.println("系统的:"+code);
        ResultVo rv = null;
        if(code.equalsIgnoreCase(imageCode)){
            //通过验证码校验-->账号和密码校验
            User user = userService.login(username, password);
            System.out.println("0代表管理员"+user.getRole());
            //判断账号和密码是否正确
            if(user!=null){
                //账号和密码正确-->判断是否是管理员
                if(0==user.getRole()){
                    //是管理员-->正常进入后台页面首页-->保存用户信息
                    session.setAttribute("user",user);
                    rv = new ResultVo(200,"登录成功",null);
                }else{
                    //非管理员登录
                    rv = new ResultVo(201,"管理员账号异常",null);
                }
            }else{
                //账号和密码不正确
                rv = new ResultVo(500,"账号或者密码错误",null);
            }
        }else{
            //响应-->验证码不正确的响应信息
            rv = new ResultVo(501,"验证码不正确",null);
        }
        //解析json并响应
        RespJsonUtils.objectToJson(rv,resp);

    }
    //分页查询
    public void queryUser(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        //获取前端页面传递的参数
        String name = req.getParameter("name");//模糊查询内容
        String curPage = req.getParameter("curPage");//当前页
        String pageSize = req.getParameter("pageSize");//页容量
        //调用分页查询
        PageBean<User> pageBean = userService.queryUser(name, Integer.valueOf(pageSize), Integer.valueOf(curPage));
        //解析json并响应
        RespJsonUtils.objectToJson(pageBean,resp);
    }
    //添加用户
    public void addUser(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        //获取表单数据
        Map<String, String[]> map = req.getParameterMap();
        User u = new User();
        BeanUtils.populate(u,map);
        //添加
        boolean flag = userService.addUser(u);
        ResultVo rv= null;
        if(flag){
            rv=new ResultVo(200,"添加成功",null);
        }else{
            rv=new ResultVo(500,"添加失败,请稍后重试",null);
        }
        //解析json并响应
        RespJsonUtils.objectToJson(rv,resp);
    }
    //查看用户详情


    public void queryById(HttpServletRequest req,HttpServletResponse resp) throws Exception {
        String uid = req.getParameter("uid");
        System.out.println(uid);
        User user = userService.queryUserById(uid);
        RespJsonUtils.objectToJson(user,resp);
    }
    //批量删除用户
    public void deleteUser(HttpServletRequest req,HttpServletResponse resp) throws Exception {
        String uids = req.getParameter("uids");
        boolean b = userService.deleteUser(uids);
        ResultVo rv = null;
        if (b){
            rv=new ResultVo(200,"删除用户信息成功",null);

        }else{
            //删除失败
            rv=new ResultVo(500,"删除用户信息失败",null);

        }
        RespJsonUtils.objectToJson(rv,resp);
    }
    //修改用户
    public void updateUser(HttpServletRequest req,HttpServletResponse resp) throws Exception {
        //获取表单数据
        Map<String, String[]> map = req.getParameterMap();
        User user = new User();
        //把map里面的值映射给user
        BeanUtils.populate(user,map);
        boolean b = userService.update(user);
        ResultVo rv =null;
        if (b){
          rv = new ResultVo(200,"修改成功",null);
        }else{
            rv = new ResultVo(500,"修改失败",null);

        }
        RespJsonUtils.objectToJson(rv,resp);

    }
}
